package xm;

import java.util.*;
import java.util.regex.*;
import java.io.*;
import java.text.NumberFormat;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
    public static void main (String args[]) throws IOException
    {
        int k = 0;
        do
        {
            Scanner sc = new Scanner(System.in);
            System.out.println("输入数字实现数字代表功能：");
            System.out.println("1.统计字母数量，计算字母所占百分比；");
            System.out.println("2.统计单词数量并展示；");
            int number = sc.nextInt();
            if(number == 1)
            {
                one();
            }
            else if(number == 2)
            {
                two();
            }
            else
            {
                System.out.println("输入错误！");
            }
            System.out.println("输入是否继续（1为继续，其他数字为退出）：");
            k = sc.nextInt();
        }while(k == 1);
    }

    private static String formattedDecimalToPercentage(double decimal)
    {
        //获取格式化对象
        NumberFormat nt = NumberFormat.getPercentInstance();
        //设置百分数精确度2即保留两位小数
        nt.setMinimumFractionDigits(2);
        return nt.format(decimal);
    }

    public static void one()
    {
        String a1;
        char a='A';
        int a2[]=new int[27];
        char b1[]=new char[26];
        char b2[]=new char[26];

        for(int i=0;i<26;i++)
        {
            b1[i]=a;
            b2[i]=(char)(a+32);
            a++;
        }


        try {
            BufferedReader in = new BufferedReader(new FileReader("D:\\git\\tortoiesGIT\\lingjun\\单词统计\\哈利波特.txt"));
            String str;
            while ((str = in.readLine()) != null)
            {
                char[] d=str.toCharArray();
                for(int i=0;i<d.length-1;i++)
                {
                    for(int j=0;j<26;j++)
                    {
                        if(b1[j]==d[i]||b2[j]==d[i])
                        {
                            a2[j]++;
                        }
                    }
                }
            }

            a2[26]=0;
            for(int i=0;i<26;i++) {
                a2[26]=a2[i]+a2[26];
            }

            for(int i=0;i<26;i++) {
                System.out.print(b1[i]);
                System.out.print("和");
                System.out.print(b2[i]);
                System.out.print("出现的次数为：");
                System.out.println(a2[i]);
                double d=(double)((double)a2[i]/(double)a2[26]);
                String result2=formattedDecimalToPercentage(d);
                System.out.println("result = "+result2);
            }
        } catch (IOException e) {}
    }

    public static void two() throws IOException
    {

        BufferedReader buf=new BufferedReader(new FileReader("D:\\git\\tortoiesGIT\\lingjun\\单词统计\\哈利波特.txt"));
        System.out.println("Read under this dir English.txt");
        StringBuffer sbuf=new StringBuffer();//缓冲字符串
        String line=null;
        while((line=buf.readLine())!=null){
            sbuf.append(line);//追加到缓冲字符串中
        }
        buf.close();//读取结束
        Pattern expression=Pattern.compile("[a-zA-Z]+");//定义正则表达式匹配单词
        String string1=sbuf.toString().toLowerCase();//转换成小写
        Matcher matcher=expression.matcher(string1);//定义string1的匹配器
        TreeMap myTreeMap=new TreeMap();//创建树映射 存放键/值对
        int n=0;//文章中单词总数
        Object word=null;//文章中的单词
        Object num=null;//出现的次数
        while(matcher.find())
        {//是否匹配单词
            word=matcher.group();//得到一个单词-树映射的键
            n++;//单词数加1
            if(myTreeMap.containsKey(word))
            {//如果包含该键，单词出现过
                num=myTreeMap.get(word);//得到单词出现的次数
                Integer count=(Integer)num;//强制转化
                myTreeMap.put(word,new Integer(count.intValue()+1));
            }
            else
            {
                myTreeMap.put(word,new Integer(1));//否则单词第一次出现，添加到映射中
            }
        }
        System.out.println("统计分析如下：");
        System.out.println("文章中单词总数"+n+"个");
        System.out.println("txt中不同单词"+myTreeMap.size()+"个");
        Iterator iter=myTreeMap.keySet().iterator();//得到树映射键集合的迭代器
        Object key=null;
        while(iter.hasNext()){//使用迭代器遍历树映射的键
            key=iter.next();
            System.out.println((String)key+":"+myTreeMap.get(key));
        }
    }

    static char ch(char c)
    {
        if(!(c>=97&&c<=122))
            c+=32;
        return c;
    }

    static String[] StatList(String str) {
        StringBuffer sb = new StringBuffer();
        HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表
        String[] slist = str.split("\\W+");
        int sum=0;
        int sum1=0;
        for (int i = 0; i < slist.length; i++)
        {
            if (!has.containsKey(slist[i])) { // 若尚无此单词
                has.put(slist[i], 1);
                sum++;
                sum1++;
            } else
            {//如果有，就在将次数加1
                Integer nCounts = has.get(slist[i]);
                has.put(slist[i],nCounts+1 );
            }
        }
        int temp=0;
        int temp1=0;
        String []a=new String[sum];
        int []b=new int[sum1];
        Iterator iterator = has.keySet().iterator();
        while(iterator.hasNext())
        {
            String word = (String) iterator.next();
            a[temp]=word;
            temp++;
        }
        return a;
    }
    static int[] StatList1(String str) {
        StringBuffer sb = new StringBuffer();
        HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表
        String[] slist = str.split("\\W+");
        int sum=0;
        int sum1=0;
        for (int i = 0; i < slist.length; i++)
        {
            if (!has.containsKey(slist[i])) { // 若尚无此单词
                has.put(slist[i], 1);
                sum++;
                sum1++;
            } else
            {//如果有，就在将次数加1
                Integer nCounts = has.get(slist[i]);
                has.put(slist[i],nCounts+1 );
            }
        }
        int temp=0;
        int temp1=0;
        String []a=new String[sum];
        int []b=new int[sum1];
        Iterator iterator = has.keySet().iterator();
        while(iterator.hasNext())
        {
            String word = (String) iterator.next();
            b[temp1]=has.get(word);
            temp1++;
        }
        return b;
    }

}